//
//  ResultViewController.m
//  Surveyer
//
//  Created by Tim on 13-8-16.
//  Copyright (c) 2013年 Tim. All rights reserved.
//

#import "ResultViewController.h"
#import "ResultSummaryViewController.h"
#import "ResultTableViewController.h"

@interface ResultViewController ()

@end

@implementation ResultViewController
@synthesize currentSurvey;

- (id)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(NSDictionary *)options
{
    if (self = [super initWithTransitionStyle:style navigationOrientation:navigationOrientation options:options])
    {
        self.delegate = self;
        self.dataSource = self;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedUpdate) name:SurveyerStoreUpdateNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = currentSurvey.title;
    
    [[SurveyerStore defaultStore] removeUncompletedSurvey];
    ResultSummaryViewController *sumVC = [[ResultSummaryViewController alloc] init];
    sumVC.currentSurvey = currentSurvey;
    ResultTableViewController *tableVC = [[ResultTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    tableVC.currentSurvey = currentSurvey;
    vcArray = @[sumVC, tableVC];
    
    [self setViewControllers:@[sumVC] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

- (void)receivedUpdate
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Update", @"iCloud Alert Title") message:NSLocalizedString(@"Data is synced from iCloud, please openned the result view again", @"iCloud alert message") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];
    [alert show];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Page view
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSInteger index = [vcArray indexOfObject:viewController];
    if (index == vcArray.count -1)
        return nil;
    return [vcArray objectAtIndex:index+1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [vcArray indexOfObject:viewController];
    if (index == 0)
        return nil;
    return [vcArray objectAtIndex:index-1];
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return vcArray.count;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
}
@end
